#!/bin/bash

## detect this dir
bin=`dirname "$0"`
bin=`cd "$bin"; pwd`

## recognize user waue
if [ "$(whoami)" != "waue"  ]; then
   echo "you are not waue, exit!!"
   exit;
fi

## ssh communication without password
if [ ! -d "/home/waue/.ssh" ];then
   echo "make ssh communication without password"
   ssh-keygen -t rsa -f /home/waue/.ssh/id_rsa -P ""
   cp /home/waue/.ssh/id_rsa.pub /home/waue/.ssh/authorized_keys
   ssh-add /home/waue/.ssh/id_rsa
fi

## check install or not
if [ -d "/home/waue/ICAS" ];then
   echo "You have ICAS already! System Exit.."
   exit 0;

fi

## Install to local
sudo cp -rf $bin /home/waue/ICAS
sudo chown -R waue:waue /home/waue/ICAS

## indicate path
PATH_etc=/home/waue/ICAS/etc
PATH_hadoop=/home/waue/ICAS/hadoop
PATH_work=/home/waue/ICAS/ICAS_workspace
PATH_in=/home/waue/ICAS/in
PATH_www=/home/waue/ICAS/www
hadoop_bin=$PATH_hadoop/bin

## apt install java ssh .. etc
sudo mv /etc/ssh/ssh_config /etc/ssh/ssh_config_bek
sudo cp $PATH_etc/ssh_config /etc/ssh/

sudo mv /etc/apt/sources.list /etc/apt/sources.list.bek
sudo cp $PATH_etc/sources.list /etc/apt/
sudo apt-get update
sudo apt-get install -y sun-java6-jdk php5 graphviz libgv-php5 ssh vim apache2

# apache setup

sudo cp $PATH_etc/httpd.conf /etc/apache2/
sudo /etc/init.d/apache2 restart\

## work backup dir
mkdir $PATH_work/all

## make more link
sudo ln -sf $PATH_hadoop /opt/hadoop
sudo ln -sf $PATH_work /home/waue/ICAS_workspace
sudo ln -sf $PATH_in /home/waue/in
#sudo ln -sf $PATH_www /var/www/demo
sudo ln -sf $PATH_www /var/www/icas

## makesure the home bin 
if [ -d /home/waue/bin ];then
   cp $bin/bin/ICAS.sh /home/waue/bin/
else
   cp -rf $bin/bin/ /home/waue/
fi

sudo mkdir /var/lib/hadoop
sudo chown waue:waue /var/lib/hadoop

## start hadoop
$hadoop_bin/hadoop namenode -format
$hadoop_bin/start-all.sh

## crontab
crontab $PATH_etc/crontab

## mkdir hdfs
sleep 3
$hadoop_bin/hadoop dfs -mkdir all
